<?php
    $counter = null;
    $noov = 1;
    $CounterModel =  ClassRegistry::init('Node.Counter');
    $visitorModel =  ClassRegistry::init('View.Visistor');
    //First we have to start the session only if it has not been started
    if(!isset($_SESSION)){
        session_start();
    }
    $session_id = session_id();//We assign the session id to the variable $session_id
    $time = time();//We assign the current time to the variable $time
    $time_limit = $time-600;//We give the session only 10 minutes if it exists
    $num = $visitorModel->find('first',array(
        'conditions' => array('Visistor.session_id' => $session_id)
    ));
    if(empty($num)){
        $save = array('session_id' => $session_id,'time' => $time);
        $visitorModel->create();
        $visitorModel->save($save);
    }else{
        $visitorModel->id = $num['Visistor']['id'];
        $visitorModel->save(array('time' => $time));
    }
    $noov  =  $visitorModel->find('count');
    //Now, check if the session was stored for more than 10 minutes and delete it if it is.
    $visitorModel->deleteAll(array('time <' => $time_limit ));
    //Lấy giá trị lượt truy cập cho tới thời điểm hiện tại    
    $counters =  ClassRegistry::init('Node.Counter')->find('first',array(
        'conditions'=>array('id'=>1),
        'fields'=>array('counter','current_online')
    ));
    if(!isset($_SESSION['Counter'])):
        $countersUpdate = $counters['Counter']['counter'];
        $countersUpdate++;
        $CounterModel->updateAll(array('Counter.counter' => $countersUpdate));
        endif;
?>
<div class="asbl-r">
    <h3 class="tit-bd">Counter</h3>
    <div class="asbl-rct">
        <ul class="counter-list">
            <li class="item-bg">
                <span class="txt-ct"><?php echo __d('default','Đang trực tuyến');?>: </span>
                <span class="txt-nb"><span class="txt-nmct"><?php echo $noov;?></span><i class="icon-cus-online"></i></span>
            </li>
            <li class="item-bg">
                <span class="txt-ct"><?php echo __d('default','Lượt truy cập');?>: </span>
                <span class="txt-nb"><span class="txt-nmct"><?php echo intval($counters['Counter']['counter']);?></span><i class="icon-cus-visit"></i></span>
            </li>
            <li class="item-bg">
                <span class="txt-ct"><?php echo __d('default','Lượt xem');?>: </span>
                <span class="txt-nb"><span class="txt-nmct">3154966</span><i class="icon-cus-view"></i></span>
            </li>
        </ul>
    </div>
</div>